﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper;
namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "负责人")]
    public class CarPartsController : Controller
    {
        const int PAGE_MAX = 8;
        CarPartsServices carPartServices = new CarPartsServices();
        //
        // GET: /CarParts/

        public ActionResult Index()
        {
            Hashtable ht = new Hashtable();
            SearchCarPartsByRequeast(ref ht);
            int page;
            if (!string.IsNullOrWhiteSpace(Request.Params["Page"]))
                page = int.Parse(Request.Params["Page"]);
            else
                page = 1;
            return View(carPartServices.SearchCarParts(ht, (page - 1) * PAGE_MAX, PAGE_MAX));
        }

        [ActionName("GetPagingInfo"),HttpGet]
        public ActionResult GetPagingInfo()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request.Params["Page"]))
                currentPage = 1;
            else
                currentPage = int.Parse(Request.Params["Page"]);

            Hashtable ht = new Hashtable();
            SearchCarPartsByRequeast(ref ht);
            int itemCount = carPartServices.CountAllCarParts(ht);
            return Json(new
            {
                TotalItems = itemCount,
                ItemsPerPage = PAGE_MAX,
                CurrentPage = currentPage,
                TotalPages = (int)Math.Ceiling((decimal)itemCount / PAGE_MAX)
            },JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 搜索车辆购件
        /// </summary>
        /// <param name="ht">条件（Hashtable）</param>
        [NonAction]
        private void SearchCarPartsByRequeast(ref Hashtable ht)
        {
            string carnum = Request.Params["carNum"];
            string carpartname = Request.Params["CarPartName"];
            string apploval = Request.Params["apploval"];

            string numForSearch = "%%";
            string carPartForSearch = "%%";
            int applovalForSearch = 3;
            string companyName = "%%";
            if (Session["CompanyName"].ToString() != null)
            {
                companyName = Session["CompanyName"].ToString();
            }
            if (apploval == "已审批")
                applovalForSearch = 1;
            else if (apploval == "未审批")
                applovalForSearch = 0;
            if (!string.IsNullOrEmpty(carnum) && carnum != "车牌号")
                numForSearch = string.Format("%{0}%", carnum);
            if (!string.IsNullOrEmpty(carpartname) && carpartname != "构件名称")
                carPartForSearch = string.Format("%{0}%", carpartname);

            ht.Add("numForSearch", numForSearch);
            ht.Add("carPartForSearch", carPartForSearch);
            ht.Add("applovalForSearch", applovalForSearch);
            ht.Add("companyName", companyName);
        }

        /// <summary>
        /// 添加购件
        /// </summary>
        /// <returns>添加购件视图</returns>
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CarParts carpart)
        {
            if (ModelState.IsValid)
            {
                carpart.CompanyName = Session["CompanyName"].ToString();
            object obj = carPartServices.CreateCarPart(carpart);
                return RedirectToAction("Index");

            }
            else
                return View(carpart);
        }

        /// <summary>
        /// 返回具体某一购件的详细信息
        /// </summary>
        /// <param name="id">某一购件的ID</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            return View(carPartServices.GetCarPartsByPartID(id));
        }

        /// <summary>
        /// 编辑用车购件信息
        /// </summary>
        /// <param name="id">购件的ID</param>
        /// <param name="carpart">购件类</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            CarParts carpart = carPartServices.GetCarPartsByPartID(id);
            return View(carpart);
        }
        [HttpPost]
        public ActionResult Edit(CarParts carpart)
        {
            if (ModelState.IsValid)
            {
                int rows = carPartServices.EditCarPart(carpart);
                return RedirectToAction("Index");
            }
            return View(carpart);
        }

        /// <summary>
        /// 删除车辆购件
        /// </summary>
        /// <param name="id">购件ID</param>
        /// <returns>删除车辆购件的视图</returns>
        public ActionResult Delete(int id)
        {
            return View(carPartServices.GetCarPartsByPartID(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {

            if (Request.IsAjaxRequest())
            {
                int i = carPartServices.DeleteCarPart(id);
                if (i == 1)//删除成功
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else//删除不成功
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }


        }


    }
}
